package com.atguigu.gmall.wms.listener;


import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.wms.entity.SkuLockVO;
import com.atguigu.gmall.wms.mapper.WareSkuMapper;
import com.rabbitmq.client.Channel;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.util.List;

@Component
public class SpuInfoListener {
    @Autowired
    StringRedisTemplate redisTemplate;
    @Autowired
    WareSkuMapper wareSkuMapper;
    private final static String KEY_PREFIX = "stock:info:";
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "item_unlock_queue",durable = "true")
            ,exchange =@Exchange(
                    value = "ORDER_CREATE_EXCHANGE",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC),
        key = {"wms.unlock" ,"stock.unlock"}))
    private void unlock(String orderToken, Channel channel, Message message) throws IOException {
        if (StringUtils.isBlank(orderToken)){
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

            return;
        }
        String skuLockVojson = redisTemplate.opsForValue().get(KEY_PREFIX + orderToken);
        if (org.apache.commons.lang3.StringUtils.isBlank(skuLockVojson)){
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            return;
        }
        List<SkuLockVO> skuLockVOList = JSON.parseArray(skuLockVojson, SkuLockVO.class);
        if (CollectionUtils.isEmpty(skuLockVOList)){
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            return;
        }
        skuLockVOList.forEach(skuLockVO -> {
           this.wareSkuMapper.unlock(skuLockVO.getWareId(),skuLockVO.getCount());
        });

    this.redisTemplate.delete(KEY_PREFIX + orderToken);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);


    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "item_minus_queue",durable = "true")
            ,exchange =@Exchange(
            value = "ORDER_PAY_EXCHANGE",
            ignoreDeclarationExceptions = "true",
            type = ExchangeTypes.TOPIC),
            key = "stock.minus"))
    private void stockMinus(String orderToken, Channel channel, Message message) throws IOException {
        if (StringUtils.isBlank(orderToken)){
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            return;
        }
        String skuLockVojson = redisTemplate.opsForValue().get(KEY_PREFIX + orderToken);
        if (org.apache.commons.lang3.StringUtils.isBlank(skuLockVojson)){
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            return;
        }
        List<SkuLockVO> skuLockVOList = JSON.parseArray(skuLockVojson, SkuLockVO.class);
        if (CollectionUtils.isEmpty(skuLockVOList)){
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            return;
        }
        //更新库存
        skuLockVOList.forEach(skuLockVO -> {
            this.wareSkuMapper.minus(skuLockVO.getWareId(),skuLockVO.getCount());
        });

        //删除锁库存的订单缓存
        this.redisTemplate.delete(KEY_PREFIX + orderToken);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

        //完成
    }

}
